package com.hxs.learn.action.core;

import java.util.List;

import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.page.Page;
import com.hxs.learn.common.page.PageWrapper;
import com.hxs.learn.common.result.DataTableResult;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.entity.core.SysPermission;
import com.hxs.learn.service.core.SysPermissionService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author kevin
 */
@RestController
@RequestMapping("core/permission")
public class PermissionAction extends AbstractViewAction {
    final private SysPermissionService permissionService;

    @Autowired
    public PermissionAction(SysPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @ApiOperation(
        value     = "删除权限数据",
        notes     = "权限管理时删除权限数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "id",
        value     = "权限ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @PostMapping(ReqConst.REQ_DELETE_DATA_BY_ID)
    @ResponseBody
    public Result deleteData(@PathVariable String id) {
        return permissionService.removeById(id);
    }

    /**
     * index
     *
     * @return
     */
    @GetMapping(ReqConst.REQ_INDEX)
    public ModelAndView index(@Login LoginInfo userInfo) {
        return createModelAndView(userInfo);
    }

    @ApiOperation(
        value      = "保存权限数据",
        notes      = "权限管理时保存权限数据",
        httpMethod = "POST",
        produces   = MediaType.APPLICATION_JSON_VALUE
    )
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyData(@Login LoginInfo loginInfo, SysPermission permission) {
        return permissionService.save(loginInfo, permission);
    }

    @ApiOperation(
        value     = "编辑权限页面",
        notes     = "权限管理时显示编辑权限页面"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "id",
        value     = "权限ID",
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping(ReqConst.REQ_MODIFY_DATA_DIALOG)
    public ModelAndView modifyDataDialog(Model model, String id) {
        SysPermission permissionPojo = permissionService.findById(id);

        model.addAttribute(PAGE_DATA, permissionPojo);

        return createModelAndView(ViewNames.PAGE_PERMISSION_MODIFY);
    }

    @ApiOperation(
        value = "获取权限列表数据",
        notes = "菜单和角色管理时获取权限列表数据"
    )
    @ResponseBody
    @GetMapping(ReqConst.REQ_QUERY_LIST_DATA)
    public List<SysPermission> queryListData() {
        return permissionService.findAllSysPermission();
    }

    @ApiOperation(
        value     = "获取权限页面数据",
        notes     = "权限管理时获取权限页面数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "name",
        value     = "权限名称",
        dataType  = "String",
        paramType = "query"
    ) , })
    @ResponseBody
    @GetMapping(ReqConst.REQ_QUERY_PAGE_DATA)
    public Result queryPageData(Page page, SysPermission permission) {
        PageWrapper<SysPermission> wrapper = permissionService.findSysPermissionPageData(page, permission);

        return DataTableResult.ok(wrapper.getPage(), wrapper.getRows());
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
